<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="header">
            <el-input placeholder="请输入内容" v-model="queryInfo.query" class="input-with-select" clearable @clear="getUserList">
                <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
            </el-input>
            <el-button type="primary" @click="addDialogVisible = true">
                添加用户
            </el-button>
        </div>
        <el-table :data="userlist" border stripe>
            <el-table-column type="index">
            </el-table-column>
            <el-table-column prop="username" label="姓名" width="90">
            </el-table-column>
            <el-table-column prop="mobile" label="电话">
            </el-table-column>
            <el-table-column prop="email" label="邮箱">
            </el-table-column>
            <el-table-column prop="role_name" label="角色" width="120">
            </el-table-column>
            <el-table-column label="状态" width="70">
                <template #default="scope">
                    <el-switch @change="userStateChanged(scope.row)" v-model="scope.row.mg_state"></el-switch>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="200">
                <template #default="scope">
                    <el-button type="primary" @click="handleClick(scope.row)" size="small" icon="el-icon-edit"></el-button>
                    <el-button type="danger" size="small" icon="el-icon-delete"></el-button>
                    <el-tooltip effect="dark" content="分配角色" placement="top">
                        <el-button type="warning" size="small" icon="el-icon-setting"></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 3, 4]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="36%" @close="addDialogClosed">
            <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="addForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="addForm.password"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addForm.email"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="mobile">
                    <el-input v-model="addForm.mobile"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="info" @click="addDialogVisible = false">取消</el-button>
                <el-button type="primary" @click="addUser">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog title="修改用户" :visible.sync="editaddDialogVisible" width="36%" @close="addDialogClosed">
            <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="addForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="addForm.password"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addForm.email"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="mobile">
                    <el-input v-model="addForm.mobile"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="info" @click="addDialogVisible = false">取消</el-button>
                <el-button type="primary" @click="addUser">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data () {
        const checkEmail = (rule, value, cb) => {
            const regEmail = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+/
            if (regEmail.test(value)) {
                return cb()
            }
            cb(new Error('请输入合法的邮箱'))
        }
        const checkMobile = (rule, value, cb) => {
            const regEmail = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
            if (regEmail.test(value)) {
                return cb()
            }
            cb(new Error('请输入正确的手机号'))
        }
        return {
            queryInfo: {
                query: '',
                // 当前的页数
                pagenum: 1,
                // 每页数据的条数
                pagesize: 2,
            },
            searchinput: '',
            userlist: [],
            total: 1,
            addDialogVisible: false,
            editaddDialogVisible: false,
            addForm: {
                username: '',
                password: '',
                email: '',
                mobile: ''
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { validator: checkEmail, trigger: 'blur' }
                ],
                mobile: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { validator: checkMobile, trigger: 'blur' }
                ],
            },
        }
    },
    created () {
        this.getUserList()
    },
    methods: {
        // 获取用户列表
        async getUserList () {
            const { data: res } = await this.$http.get('users', { params: this.queryInfo })
            console.log('获取用户列表', res);
            if (res.meta.status !== 200) return this.$message.error('获取用户列表失败!')
            this.userlist = res.data.users
            this.total = res.data.total
        },
        // 监听显示数据条数变化
        handleSizeChange (newsize) {
            this.queryInfo.pagesize = newsize
            this.getUserList()
        },
        // 监听页码值改变
        handleCurrentChange (newPage) {
            this.queryInfo.pagenum = newPage
            this.getUserList()
        },
        // 更新用户状态
        async userStateChanged (userinfo) {
            const { data: res } = await this.axios.put(`users/${userinfo.id}/state/${userinfo.mg_state}`)
            if (res.meta.status !== 200) return this.$message.error('更新用户状态失败')
            this.$message.success('更新用户状态成功!')
        },
        // 关闭时重置表单
        addDialogClosed () {
            this.$refs.addForm.resetFields()
        },
        // 添加用户
        addUser () {
            this.$refs.addForm.validate(async valid => {
                if (!valid) return
                const { data: res } = await this.$http.post("users", this.addForm)
                if (res.meta.status !== 201) {
                    this.addDialogVisible = false
                    this.$message.error('添加用户失败')
                }
                this.addDialogVisible = false
                this.$message.success('添加用户成功')
                this.getUserList()
            })
        },
        // 修改对话框
        handleClick () {

        }
    }
}
</script>

<style lang="less" scoped>
.el-dialog {
    .el-form {
        .el-form-item {
            margin-right: 50px;
        }
    }
    .dialog-footer {
        margin-right: 50px;
    }
}
</style>